<script setup lang="ts" name="DictDataEdit">
import { LIST_CLASS_COLUMN, LIST_CLASS_ENUM } from "@/views/system/dict/const";
import { SYS_NORMAL_DISABLE_ENUM } from "@/global/const";
import { addDictDataApi, getDictDataApi, updateDictDataApi } from "@/api/system/dictData";

const { proxy } = getCurrentInstance();

const { sys_normal_disable } = useDict("sys_normal_disable");

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  id: {
    default: null,
  },
  title: {
    type: String,
    default: '',
  },
  dictType: {
    type: String,
    default: '',
  }
});

// 字典值管理表单
const dictDataFormRef = ref(null);

// 弹窗开关
const open = ref(false);

// 表单数据
const form = ref<DictDataRecord>({});

// 表单校验
const rules = ref({
  dictCode: [
    { required: true, message: "字典编号不能为空", trigger: "blur" },
  ],
  dictType: [
    { required: true, message: "字典类型不能为空", trigger: "blur" },
  ],
  dictLabel: [
    { required: true, message: "数据标签不能为空", trigger: "blur" },
  ],
  dictValue: [
    { required: true, message: "数据键值不能为空", trigger: "blur" },
  ],
  dictSort: [
    { required: true, message: "显示排序不能为空", trigger: "blur" },
  ],
  status: [
    { required: true, message: "状态不能为空", trigger: "change" },
  ],
});

// 加载
const loading = ref(false);

/** 获取字典值管理详情 */
function getData() {
  reset();
  if (isEmpty(props.id)) {
    open.value = true;
  } else {
    getDictDataApi(props.id).then(response => {
      form.value = response.data;
      open.value = true;
    }).catch(() => {
      closeDialog(false);
    })
  }
}

/** 表单重置 */
function reset() {
  form.value = {
    dictCode: null,
    dictType: props.dictType,
    dictLabel: null,
    dictValue: null,
    cssClass: null,
    dictSort: 1,
    listClass: null,
    status: SYS_NORMAL_DISABLE_ENUM.normal,
    remark: null,
  };
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 提交按钮 */
function submitForm() {
  // @ts-ignore
  dictDataFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      if (isEmpty(form.value.dictCode)) {
        addDictDataApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("新增成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false;
        });
      } else {
        updateDictDataApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("修改成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false;
        });
      }
    }
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
});

getData();
</script>

<template>
  <!-- 字典值管理编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="700px" append-to-body :close-on-click-modal="false">
    <el-form ref="dictDataFormRef" :model="form" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="form.dictType" disabled placeholder="请输入字典类型" />
      </el-form-item>
      <el-form-item label="回显样式" prop="listClass">
        <el-select v-model="form.listClass" clearable placeholder="请选择回显样式">
          <el-option
              :label="item.label"
              :value="item.value"
              v-for="(item, index) in LIST_CLASS_COLUMN"
              :key="index"
          >
            <span v-if="item.value == LIST_CLASS_ENUM.default || item.value == ''">{{ item.label }}</span>
            <el-tag v-else :type="item.value">{{ item.label }}</el-tag>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
      </el-form-item>
      <el-form-item label="数据键值" prop="dictValue">
        <el-input v-model="form.dictValue" placeholder="请输入数据键值" />
      </el-form-item>
      <el-form-item label="样式属性" prop="cssClass">
        <el-input v-model="form.cssClass" placeholder="请输入样式属性" />
      </el-form-item>
      <el-form-item label="显示排序" prop="dictSort">
        <el-input-number v-model="form.dictSort" :min="1" placeholder="请输入显示排序" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status" placeholder="请选择状态">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_normal_disable"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark" class="item-describe">
        <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>